package com.qq.BFMRSE.index;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.commons.collections.bag.TreeBag;

import com.qq.BFMRSE.entity.BloomFilter;
import com.qq.BFMRSE.entity.indexTreeNode;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;


public class indexTreeUpLoad {

	public static String getIndexTreeInfo(indexTreeNode root)
	{
		JsonConfig jsonConfig=new JsonConfig();
		jsonConfig.setExcludes(new String[]{"bf","parent"});
		JSONObject tempjO=JSONObject.fromObject(root,jsonConfig); 
		JSONObject jO=(JSONObject) JSONSerializer.toJSON(tempjO.toString(), jsonConfig);
		String indexTreeInfo=jO.toString();
		return indexTreeInfo;
	}

	public  static String getIndexTreeBfs(List<String>fileNameList)
	{
		Queue<indexTreeNode>queue=buildIndexTreeHelper.getallLeafIndexTree(fileNameList);
		Map<String,String> resultMap=new HashMap<String, String>();
		while(!queue.isEmpty())
		{
			indexTreeNode leafNode=queue.poll();
			BloomFilter leafBf=leafNode.getBf();
			resultMap.put(leafNode.getFID(), leafBf.getBits().toString());
		}
		JSONObject jo=JSONObject.fromObject(resultMap);
		return jo.toString();
		
	}
	public static List<Integer>paraseIndexOfBit(String bitString)
	{
		List<Integer>list=new ArrayList<Integer>();
		String []temp=bitString.substring(1, bitString.length()-1).split(",");
		for(int i=0;i<temp.length;i++)
		{
			Integer integer=new Integer(temp[i].trim());
			list.add(integer);
		}
		return list;
	}
	public static Map<String, List<Integer>>Json2MapBitSet(String jsonString)
	{
		Map<String, List<Integer>>resultMap=new HashMap<String, List<Integer>>();
		JSONObject jObject=JSONObject.fromObject(jsonString);
		Iterator<String>iterator=jObject.keys();
		while(iterator.hasNext())
		{
			String tempKey=iterator.next();
			List<Integer>tempList=paraseIndexOfBit(jObject.get(tempKey).toString());
			resultMap.put(tempKey,tempList);
		}
		return resultMap;
	}
}
